home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / admin / linuxcon.000 / linuxcon / linuxconf-1.6 / netconf / thishost1.c < prev   
C/C++ Source or Header  |  1996-03-24  |  5KB  |  196 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <unistd.h>
  4. #include <errno.h>
  5. #include <sys/stat.h>
  6. #include <sys/types.h>
  7. #include <time.h>
  8. #include <netdb.h>
  9. #include "netconf.h"
  10. #include "../userconf/userconf.h"
  11. #include "../xconf/xconf.h"
  12. #include "../paths.h"
  13. #include "netconf.m"
  14. /*
  15.     Try to find out if networking is somewhat configured
  16.     Returne the host entry of the this host or NULL if not minimally
  17.     configured.
  18. */
  19. struct hostent *netconf_netok(char *status)
  20. {
  21. #if 0
  22.     struct stat buf;
  23.     struct hostent *ret = NULL;
  24.     static char *hostfile = ETC_HOSTNAME;
  25.     strcpy (status,hostfile);
  26.     strcat (status," do not exist\n");
  27.     if (stat(hostfile,&buf) != -1){
  28.         strcpy (status,hostfile);
  29.         strcat (status," do exist\n");
  30.         FILE *fin = fopen (hostfile,"r");
  31.         if (fin == NULL){
  32.             strcat (status,"but can't be read\n");
  33.         }else{
  34.             char host[200];
  35.             if (fscanf (fin,"%s",host)!=1){
  36.                 strcat (status,"but do not contain the name of the system\n");
  37.             }else if ((ret=gethostbyname(host))==NULL){
  38.                 strcat (status,"but the name of the system, ");
  39.                 strcat (status,host);
  40.                 strcat (status,"\nhas no definition in /etc/hosts\n");
  41.             }
  42.             fclose (fin);
  43.         }
  44.     }
  45.     return ret;
  46. #else
  47.     struct hostent *ret = gethostbyname(NAM_ETH0_LOGHOST);
  48.     status[0] = '\0';
  49.     if (ret==NULL){
  50.         strcpy (status,
  51.             MSG_U(E_NOPRIMNAME
  52.              ,"No definition for the primary name\n"
  53.              "in /etc/hosts.\n"
  54.              "There is no entry with the name or alias\n"
  55.              "\"loghost\".\n"));
  56.     }
  57.     return ret;
  58. #endif
  59. }    
  60.  
  61. static void thishost1_fqhn2host (const char *fqhn, char *hostpart)
  62. {
  63.     char *pt = strchr(fqhn,'.');
  64.     if (pt != NULL){
  65.         int len = (int)(pt-fqhn);
  66.         memcpy (hostpart,fqhn,len);
  67.         hostpart[len] = '\0';
  68.     }else{
  69.         strcpy (hostpart,fqhn);
  70.     }
  71. }
  72.  
  73.  
  74. static int thishost1_sethostname (const char *str)
  75. {
  76.     int ret = -1;
  77.     if (perm_rootaccess (MSG_U(P_SETHOSTNAME,"Setting the hostname"))){
  78.         char buf[100];
  79.         thishost1_fqhn2host (str, buf);
  80.         ret  = sethostname (buf,strlen(buf)+1);
  81.     }
  82.     return ret;
  83. }
  84.  
  85. /*
  86.     Set the hostname in the kernel.
  87.     Return -1 if anything goes wrong.
  88. */
  89. int netconf_sethostname()
  90. {
  91.     THISHOST thishost;
  92.     int ret = -1;
  93.     if (thishost.configok()){
  94.         const char *str = thishost.getname1();
  95.         char hostpart[100];
  96.         thishost1_fqhn2host (str,hostpart);
  97.         char buf[100];
  98.         if (gethostname(buf,sizeof(buf)-1)!=-1
  99.             && strcmp(buf,hostpart)==0){
  100.             ret = 0;
  101.         }else{
  102.             if (simul_ison()){
  103.                 simul_addmsg (MSG_U(X_SETHOSTNAME
  104.                     ,"Setting hostname to %s\n"),str);
  105.                 ret = 0;
  106.             }else{
  107.                 ret = thishost1_sethostname (str);
  108.                 if (ret == -1){
  109.                     xconf_error (MSG_U(E_CANTSETHOST
  110.                         ,"Can't set the host name to %s\n(%s)\n")
  111.                         ,str,strerror(errno));
  112.                 }else{
  113.                     net_prtlog (MSG_R(X_SETHOSTNAME),str);
  114.                 }
  115.             }
  116.         }
  117.     }
  118.     return ret;
  119. }
  120. /*
  121.     Clone of the /bin/hostname command
  122. */
  123. int netconf_hostname (int argc, char *argv[])
  124. {
  125.     /* #Specification: netconf / aliases / hostname
  126.         netconf emulate the command /bin/hostname. Without argument
  127.         it print the hostname. With one, it set the hostname.
  128.         Option -f may be used to force a lookup of the second argument
  129.         and use the first value returned by gethostbyname().
  130.  
  131.         The argument may be a fully qualified name. Only the host
  132.         part is used.
  133.     */
  134.     int ret = -1;
  135.     if (argc == 1){
  136.         char buf[100];
  137.         if (gethostname(buf,sizeof(buf)-1)==-1) strcpy (buf,"none");
  138.         printf ("%s\n",buf);
  139.         ret = 0;
  140.     }else if (argc == 2
  141.         && (strcmp(argv[1],"-d")==0 || strcmp(argv[1],"--domain")==0)){
  142.         THISHOST host;
  143.         const char *n = host.getname1();
  144.         const char *pt = strchr(n,'.');
  145.         if (pt != NULL){
  146.             pt++;
  147.         }else{
  148.             pt = "";
  149.         }
  150.         printf ("%s\n",pt);
  151.         ret = 0;
  152.     }else if (argc == 2 && argv[1][0] != '-'){
  153.             ret = thishost1_sethostname (argv[1]);
  154.     }else if (argc == 3 && strcmp(argv[1],"-f")==0){
  155.         char *hostname = argv[2];
  156.         struct hostent *ent = gethostbyname(hostname);
  157.         if (ent == NULL){
  158.             xconf_error (MSG_U(E_NOTKNOWN,"%s is not known\n"),hostname);
  159.         }else{
  160.             ret = thishost1_sethostname (ent->h_name);
  161.         }
  162.     }else{
  163.         fprintf (stderr,MSG_U(E_HOSTUSAGE
  164.             ,"usage: hostname [-f] [ host name ]\n"
  165.              "       hostname -d\n"
  166.              "       hostname --domain\n"));
  167.     }
  168.     return ret;
  169. }
  170.  
  171. /*
  172.     Clone of the /bin/dnsdomainname command
  173. */
  174. int netconf_dnsdomainname (int argc, char *[])
  175. {
  176.     /* #Specification: netconf / aliases / hostname
  177.         netconf emulate the command /bin/dnsdomainname. Without argument
  178.         it print the dns domain name. With one, it set the hostname.
  179.         Option -f may be used to force a lookup of the second argument
  180.         and use the first value returned by gethostbyname().
  181.  
  182.         The argument may be a fully qualified name. Only the host
  183.         part is used.
  184.     */
  185.     int ret = -1;
  186.     if (argc == 1){
  187.         static char *tb[]={"","-d",NULL};
  188.         ret = netconf_hostname (2,tb);
  189.     }else{
  190.         fprintf (stderr,MSG_U(E_DNSDOMUSAGE
  191.             ,"usage: dnsdomainname\n(No argument)\n"));
  192.     }
  193.     return ret;
  194. }
  195.  
  196.